unit ufrmMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ZConnection, uDMConexao, Menus, ufrmMdiChildPadrao, ImgList, Db, DBGrids,
  ExtCtrls, StdCtrls, IBServices, AppBar, Buttons, ShellAPI, ufrmCadPadrao;

const
  WM_ICONTRAY = WM_USER + 1;

type

  TfrmMain = class(TAppBar)
    imglist: TImageList;
    ilMenu: TImageList;
    imgBg: TImage;
    pmCadastro: TPopupMenu;
    Autores2: TMenuItem;
    Editoras2: TMenuItem;
    Obras2: TMenuItem;
    pTray: TPopupMenu;
    Ativar1: TMenuItem;
    Esconder1: TMenuItem;
    Sair1: TMenuItem;
    btnCadastro: TSpeedButton;
    btnSair: TSpeedButton;
    Image1: TImage;
    procedure FormCreate(Sender: TObject);
    procedure Fechar1Click(Sender: TObject);
    procedure Autores2Click(Sender: TObject);
    procedure Editoras2Click(Sender: TObject);
    procedure Obras2Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btnSairClick(Sender: TObject);
    procedure btnCadastroClick(Sender: TObject);
    procedure Sair1Click(Sender: TObject);
    procedure Esconder1Click(Sender: TObject);
    procedure Ativar1Click(Sender: TObject);
  private
    TrayIconData: TNotifyIconData;
    { Private declarations }
  public
    { Public declarations }
    procedure TrayMessage(var Msg: TMessage); message WM_ICONTRAY;
    procedure MostrarTela(Sender: TObject; Classe: TComponentClass; F: Pointer;
      FuncaoRetorno: TFuncaoRetorno = nil; MantemReferencia: Boolean = true; maximized: Boolean = false);
    function DesenharCampoBooleano(Grid: TDbGrid; Column: tcolumn;
      Rect: TRect; DesenharFalso: Boolean = false): Boolean;
    function capitalize(str: string): string;
    procedure CapitalizeDataField(field: TField);
  end;

var
  frmMain: TfrmMain;

implementation

uses ufrmCadAutor, ufrmCadObra,
  ufrmCadEditoraEd, ufrmCadEditora;

{$R *.dfm}

function TfrmMain.DesenharCampoBooleano(Grid: TDbGrid; Column: tcolumn;
  Rect: TRect; DesenharFalso: Boolean = false): Boolean;
var
  x, y: Integer;
begin
  Result := Column.Field is TBooleanField;
  if Result then
  begin
    Grid.Canvas.FillRect(Rect);
    x := (Rect.Left + Rect.Right - imglist.Width) div 2;
    y := (Rect.Top + Rect.Bottom - imglist.Height) div 2;
    if Column.Field.AsBoolean then
      imglist.Draw(Grid.Canvas, x, y, 0)
    else if DesenharFalso then
      imglist.Draw(Grid.Canvas, x, y, 1);
  end;
end;

procedure TfrmMain.MostrarTela(Sender: TObject; Classe: TComponentClass; F: Pointer;
  FuncaoRetorno: TFuncaoRetorno = nil; MantemReferencia: Boolean = true; maximized: Boolean = false);
var
  Frm: TForm;
begin
  if Pointer(F^) = nil then
    Application.CreateForm(Classe, F^);
  Frm := TForm(F^);

  if (frm is TfrmMdiChildPadrao) then
  begin
    if (Sender is TControl) then
      if Sender <> frmMain then
        (Frm as TfrmMdiChildPadrao).CallerControl := Sender as TControl;
    if Assigned(FuncaoRetorno) then
      (frm as TfrmMdiChildPadrao).OnRetorno := FuncaoRetorno;
    if MantemReferencia then
      (Frm as TfrmMdiChildPadrao).Variavel := F
    else
      (Frm as TfrmMdiChildPadrao).Variavel := nil;
  end;

  if maximized then
    Frm.WindowState := wsMaximized
  else
    Frm.WindowState := wsNormal;

  Frm.Position := poScreenCenter;
  Frm.show;
  Frm.BringToFront;
end;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  if FileExists('..\imagens\logo.bmp') then
    imgBg.Picture.LoadFromFile('..\imagens\logo.bmp');
  Edge := abeRight;
  Width := 80;
  Self.AlwaysOnTop := true;
  Realign;
  VertDockSize := 80;

  with TrayIconData do
  begin
    cbSize := SizeOf(TrayIconData);
    Wnd := Handle;
    uID := 0;
    uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;
    uCallbackMessage := WM_ICONTRAY;
    hIcon := Application.Icon.Handle;
    StrPCopy(szTip, Application.Title);
  end;

  Shell_NotifyIcon(NIM_ADD, @TrayIconData);
  
  btnSair.Top := Height - btnSair.Height - 50;
end;

procedure TfrmMain.TrayMessage(var Msg: TMessage);
var p: TPoint;
begin
  case Msg.lParam of
    WM_LBUTTONDOWN:
      begin
//        Redimensionar;
{      ShowMessage('Left button clicked - let''s SHOW the Form!');
      Form1.Show;}
      end;
    WM_RBUTTONDOWN:
      begin
        SetForegroundWindow(Handle);
        GetCursorPos(p);
        pTray.Popup(p.x, p.y);
        PostMessage(Handle, WM_NULL, 0, 0);
      end;
  end;
end;

procedure TfrmMain.Fechar1Click(Sender: TObject);
begin
  Close;
end;

procedure TfrmMain.Autores2Click(Sender: TObject);
begin
  MostrarTela(Self, TfrmCadAutor, @frmCadAutor);
end;

procedure TfrmMain.Editoras2Click(Sender: TObject);
begin
  MostrarTela(Self, TfrmCadEditora, @frmCadEditora);
end;

procedure TfrmMain.Obras2Click(Sender: TObject);
begin
  MostrarTela(Self, TfrmCadObra, @frmCadObra, nil, true);
end;

procedure TfrmMain.FormDestroy(Sender: TObject);
begin
  Shell_NotifyIcon(NIM_DELETE, @TrayIconData);
end;

procedure TfrmMain.btnSairClick(Sender: TObject);
begin
  close;
end;

procedure TfrmMain.btnCadastroClick(Sender: TObject);
var
  p: TPoint;
begin
  p.x := btnCadastro.Left;
  p.y := btnCadastro.Top + 10; // + SpeedButton1.Height + 1;
  p := ClientToScreen(p);
  pmCadastro.Popup(p.x, p.y);
end;

function TfrmMain.capitalize(str: string): string;
var bCharPos, i: Integer;
  tmpStr: string;
begin
  str := Trim(str);
  if str = '' then
    Exit;
  i := 0;
  str[i] := UpCase(str[i]);
  for i := 0 to Length(str) - 1 do
  begin
    if (str[i - 1] = ' ') or (str[i - 1] = #0) then
      if (Length(Trim(Copy(str, i - 1, 3))) = 1) then
        Continue
      else if (Length(Trim(Copy(str, i - 1, 4))) > 2) then
        str[i] := UpCase(str[i]);
  end;
  Result := str;
end;

procedure TfrmMain.Sair1Click(Sender: TObject);
begin
  close;
end;

procedure TfrmMain.CapitalizeDataField(field: TField);
begin
  if (field <> nil) and (field.DataSet.State in [dsInsert, dsEdit]) then
      if field.DataType = ftString then
        field.AsString := capitalize(field.AsString);
end;

procedure TfrmMain.Esconder1Click(Sender: TObject);
begin
  self.VertDockSize := 5;
  UpdateBar;
end;

procedure TfrmMain.Ativar1Click(Sender: TObject);
begin
  self.VertDockSize := 80;
  UpdateBar;
end;

end.

